<?php
namespace app\admin\controller;
use \think\Db;
use app\admin\service\PayeeOrderService;
use think\Controller;
//个人会员处理
/**
 * Class PayeeOrder 商家管理控制器
 * @package app\admin\controller
 */
class PayeeOrder extends Controller
{       
    //订单列表
    public function index()
    {
        $param = $this->request->param();
        $where = [];
        //条件查询
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.addtime'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.addtime'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.addtime'] = ['<= time', $endTime];
            }
        }
        $userName   = empty($param['userName']) ? '' : $param['userName'];
        if (!empty($userName)) {
            $where['a.userId'] = db::name('users')->where('userName',$userName)->value('userId');
        }
        $shopName   = empty($param['shopName']) ? '' : $param['shopName'];
        if (!empty($shopName)) {
            $where['a.shopId'] = db::name('shop')->where('shopName',$shopName)->value('shopId');
        }
        $wxOrder   = empty($param['wxOrder']) ? '' : $param['wxOrder'];
        if (!empty($wxOrder)) {
            $where['a.wxOrder'] = $wxOrder;
        }
        $orderNumber   = empty($param['orderNumber']) ? '' : $param['orderNumber'];
        if (!empty($orderNumber)) {
            $where['a.orderNumber'] = $orderNumber;
        }
        //dump($where);exit;
        $rs=db::name('order')->alias('a')
        ->join('mj_shop b','a.shopId=b.shopId','left')
        ->join('mj_staff c','a.staffId=c.userId','left')
        ->join('mj_users d','a.userId=d.userId','left')
        ->field('a.*,b.shopName,c.userName as staffName,d.userName')
        ->where($where)
        ->order('addtime desc')
        ->paginate(10,false,['query'=>$param]);

        $items = $rs->items();
        foreach ($items as $k=>$v) {
             $arr = explode(",",$v['commodityId']);
             $items[$k]['commodityId'] = Db::name('shop_config')->where('id','in',$arr)->field('name')->select();

        }
        //dump($items);exit;
        $this->assign("filter", $param);//设置过滤条件
        $this->assign('arr',$rs);
        $this->assign('items',$items);
        return $this->fetch();
    }


    function statusPay(){
        $param = $this->request->param();
        if($param['status']=='1'){
            db::name('order')->where('id',$param['id'])->setField('status',3);
        }else{
            db::name('order')->where('id',$param['id'])->setField('status',5);
        }
        return json(['code'=>200,'msg'=>'成功！']);

    }
}
